﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.DataModel;
using Giaiphongmatbang.BLL;
namespace Giaiphongmatbang
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }
        QLGPMBEntities db = new QLGPMBEntities();
        IEnumerable<Land> getParent()
        {
            return db.Lands.Where(p => p.ParentID == 0);
        }

        IEnumerable<Land> getChild(int ParentID)
        {
            return db.Lands.Where(p => p.ParentID == ParentID);
        }

        string img(int id)
        {
            if (!db.Lands.Any(p => p.ParentID == id))
                return "<img src='/Images/minus.gif' align='bottom'/>";
            return "<img src=''/>";
        }
        protected void BindData()
        {
            StringBuilder str = new StringBuilder("<table class='treetable' width='100%' border='1' color='red'>");
            str.Append("<tr><th>Tên loại đất</th><th>Vị trí</th><th>Thửa</th><th>TBĐ</th><th>ĐVT</th><th>Giá/1 ĐVT</th><th>Sửa</th><th>Xóa</th></tr>");
            var list = getParent();
            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr><td>" + img(item.LandID) + item.LandName + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.Position + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.Plot + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.ofMap + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.Unit + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.UnitPrice + "</td>");
                node.Append("<td style=\"text-align: center;\"><a href='javascript:abc()'><img src='/Images/pen.png'/></td>");
                node.Append("<td style=\"text-align: center;\"><a href='javascript:abc()'><img src='/Images/delete.png'/></td>");
                node.Append("</tr>");
                BindDataChild(node, item.LandID,"");
                str.AppendLine(node.ToString());
            }
            str.AppendLine("</table");
            ltr.Text = str.ToString();

        }
        protected void BindDataChild(StringBuilder parent, int ParentID,string indent)
        {
            var list = getChild(ParentID);
            indent += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr><td>" + indent + img(item.LandID) + item.LandName + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.Position + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.Plot + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.ofMap + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.Unit + "</td>");
                node.Append("<td style=\"text-align: center;\">" + item.UnitPrice + "</td>");
                node.Append("<td style=\"text-align: center;\"><a href='javascript:abc()'><img src='/Images/pen.png'/></td>");
                node.Append("<td style=\"text-align: center;\"><a href='javascript:abc()'><img src='/Images/delete.png'/></td></tr>");
                BindDataChild(node, item.LandID, indent);
                parent.AppendLine(node.ToString());
            }           
        }        
    }
}